﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.DirectX.DirectInput;

namespace R10App
{
    public class JoyState
    {
        public double AxisX;
        public double AxisY;
        public double AxisZ;
        public double Throttle;
        public double Trim1;
        public double Trim2;
        public double Hat1;
        public double Hat2;
        public bool Button1;
        public bool Button2;
        public bool Button3;
    }
    class JoystickHandler
    {
        private Device joystickDevice;
        private JoystickState joyState;

        public bool Init(IntPtr hWnd)
        {
            // Find all the GameControl devices that are attached.
            DeviceList gameControllerList = Manager.GetDevices(DeviceClass.GameControl, EnumDevicesFlags.AttachedOnly);
            // check that we have at least one device.
            if (gameControllerList.Count > 0)
            {
                // Move to the first device
                gameControllerList.MoveNext();
                DeviceInstance deviceInstance = (DeviceInstance)gameControllerList.Current;

                // create a device from this controller.
                joystickDevice = new Device(deviceInstance.InstanceGuid);
                joystickDevice.SetCooperativeLevel(hWnd, CooperativeLevelFlags.Background | CooperativeLevelFlags.NonExclusive);
            }
            else
            {
                return false;
            }

            // Tell DirectX that this is a Joystick.
            joystickDevice.SetDataFormat(DeviceDataFormat.Joystick);
            // Finally, acquire the device.
            joystickDevice.Acquire();

            return true;
        }

        public JoyState Poll()
        {
            try
            {
                // poll the joystick
                joystickDevice.Poll();
                // update the joystick state field
                joyState = joystickDevice.CurrentJoystickState;

                JoyState s = new JoyState();
                s.AxisX = joyState.X / 32768.0 - 1;
                s.AxisY = 1 - joyState.Y / 32768.0;
                s.AxisZ = joyState.Rz / 32768.0 - 1;

                s.Throttle = 1 - joyState.GetSlider()[0] / 65536.0;
                //s.Throttle = 1-joyState.Z / 65536.0;
                s.Trim1 = joyState.Ry / 32768.0 - 1;
                s.Trim2 = joyState.Rx / 32768.0 - 1;
                s.Hat1 = joyState.GetPointOfView()[0] / 100.0;
                if (s.Hat1 == -0.01) s.Hat1 = -1;
                s.Button1 = joyState.GetButtons()[0] != 0;
                s.Button2 = joyState.GetButtons()[2] != 0;
                s.Button3 = joyState.GetButtons()[3] != 0;

                s.Hat2 = -1;
                if (joyState.GetButtons()[19] != 0) s.Hat2 = 0;
                if (joyState.GetButtons()[20] != 0) s.Hat2 = 90;
                if (joyState.GetButtons()[21] != 0) s.Hat2 = 180;
                if (joyState.GetButtons()[22] != 0) s.Hat2 = 270;

                return s;
            }
            catch
            {
                // we probably lost connection to the joystick
                // was it unplugged or locked by another application?
            }
            return null;
        }


    }
}
